package com.jdk18.lambda;

import org.junit.Test;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created by xuyiwei on 2018/7/9.
 */
public class DemoListToMap {
    List<Student> list = Arrays.asList(new Student(1, 18, "阿龙" ),
            new Student(2, 17, "小花" ),
            new Student(3, 17, "阿浪" ),
            new Student(3, 17, "阿浪" )
            );
    @Test
    public void listToMapByObjectValue(){
        // value 为对象 student -> student jdk1.8返回当前对象
        Map<Integer, Student> map = list.stream().collect(Collectors.toMap(Student::getId, student -> student));
        // 遍历打印结果
        map.forEach((key, value) -> {
            System.out.println("key: " + key + "    value: " + value);
        });
    }
    @Test
    public void listToMapByNameValue(){
        // value 为对象中的属性
        Map<Integer, String> map = list.stream().collect(Collectors.toMap(Student::getId, Student::getName, (a, b) -> b));
        map.forEach((key, value) -> {
            System.out.println("key: " + key + "    value: " + value);
        });
    }


}
